package com.platform.comm.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.Assert;

import java.beans.FeatureDescriptor;
import java.util.Collection;
import java.util.stream.Stream;

/**
 * Description:
 *
 * @author coder
 * Created on 2019/4/19
 **/
public final class BeanPropertyUtil {

    public static String[] getNullValPropertyNames(Object source, final Collection<String> includeFields) {
        Assert.state(source != null, "object must not be null!");
        Assert.state(includeFields != null, "include Fields List must not be null!");

        final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
        return Stream.of(wrappedSource.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(propertyName ->
                        wrappedSource.getPropertyValue(propertyName) == null && !includeFields.contains(propertyName))
                .toArray(String[]::new);
    }

    /**
     * 获取值为空属性列表
     *
     */
    public static String[] getNullValPropertyNames(Object source) {
        Assert.state(source != null, "object must not be null!");

        final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
        return Stream.of(wrappedSource.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
                .toArray(String[]::new);
    }
}
